using System;
using DevExpress.Xpo;
using SmartPeople.Framework.SecuritySpace;

namespace SmartPeople.Framework.Security.Providers.XPO
{
    [Persistent("SecurityRole")]
    internal class XPSecurityRole : XPCustomObject, ISecurityRole
    {
        public XPSecurityRole(Session session) : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here.
        }

        //public XPSecurityRole(Guid securityApplicationID, string name)
        //{
        //    //gID = Guid.NewGuid();
        //    this.name = name;
        //    creationDate = DateTime.Now;
        //    this.securityApplicationID = securityApplicationID;
        //}


        public XPSecurityRole()
        {
        }

        #region Fields

        [Key(AutoGenerate = true)]
        [Persistent("GID")]
        private Guid gID;

        //[Indexed(Unique = true)]
        [Indexed("securityApplicationID", Unique = true, Name = "IX_SRoleNameSApplicationID")]
        [Persistent("Name")]
        private string name;

        [Persistent("Description")]
        private string description;

        [Persistent("Comment")]
        private string comment;

        [Persistent("CreationDate")]
        private DateTime creationDate;

        //not from interface
        [Persistent("SecurityApplicationID")]
        private Guid securityApplicationID;

        [Persistent("IsReadOnly")]
        private bool isReadOnly;

        #endregion

        #region Properties

        [NonPersistent]
        public Guid GID
        {
            get { return gID; }
            set
            {
                if (gID != value)
                {
                    Guid OldValue = gID;
                    gID = value;
                    OnChanged("GID", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    string OldValue = name;
                    name = value;
                    OnChanged("Name", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public string Description
        {
            get { return description; }
            set
            {
                if (description != value)
                {
                    string OldValue = description;
                    description = value;
                    OnChanged("Description", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public string Comment
        {
            get { return comment; }
            set
            {
                if (comment != value)
                {
                    string OldValue = comment;
                    comment = value;
                    OnChanged("Comment", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public DateTime CreationDate
        {
            get { return creationDate; }
            set
            {
                if (creationDate != value)
                {
                    DateTime OldValue = creationDate;
                    creationDate = value;
                    OnChanged("CreationDate", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public bool IsReadOnly
        {
            get { return isReadOnly; }
            set
            {
                // The old value of the Name property.
                bool oldValue = isReadOnly;
                if (oldValue == value)
                    return;
                isReadOnly = value;
                // Keep track of every change made to the Name property.
                OnChanged("IsReadOnly", oldValue, value);
            }
        }


        //This non persistent property is needed for query purposes for XPO.
        [NonPersistent]
        public Guid SecurityApplicationID
        {
            get { return securityApplicationID; }
            //set { securityApplicationID = value; }
            set
            {
                if (securityApplicationID != value)
                {
                    Guid OldValue = securityApplicationID;
                    securityApplicationID = value;
                    OnChanged("SecurityApplicationID", OldValue, value);
                }
            }
        }


        [NonPersistent]
        internal Guid SecurityApplication
        {
            get { return securityApplicationID; }
        }

        //removed agg
        //[Aggregated,Association("SecurityRoleUsers", typeof (XPSecurityUser), UseAssociationNameAsIntermediateTableName = true)]
        [Association("SecurityRoleUsers", typeof (XPSecurityUser), UseAssociationNameAsIntermediateTableName = true)]
        internal XPCollection<XPSecurityUser> Users
        {
            get { return GetCollection<XPSecurityUser>("Users"); }
        }

        #endregion
    }
}